package com.bum.rss.site;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.InitializingBean;

import com.bum.base.util.Base64Utils;
import com.bum.rss.exp.om.RssEntry;
import com.bum.rss.site.cat.AttitudeCat;
import com.bum.rss.site.util.BaseParser;

public class AttitudeSite extends Site implements AttitudeCat, InitializingBean {
	final HashMap<String, String>	CAT_CODES	= new HashMap<String, String>();
	final List<RssEntry>			CATEGORIES	= new ArrayList<RssEntry>(CATS.length);

	@Override
	public void afterPropertiesSet() throws Exception {
		for (int n = CATS.length, i = 0; i < n; i++) {
			CAT_CODES.put(CATS[i][0], CATS[i][2]);
			CATEGORIES.add(new RssEntry(getCode() + URL_SEPARATOR + CATS[i][0], CATS[i][1]));
		}
	}

	@Override
	public List<RssEntry> listSiteCats(String user) {
		return CATEGORIES;
	}

	@Override
	public List<RssEntry> listCatEntrys(String cat, String user) {
		int page = 1;
		if (cat.indexOf(PAGE_SEPARATOR) > 0) {
			String[] parts = StringUtils.split(cat, PAGE_SEPARATOR);
			page = NumberUtils.toInt(parts[1], 1);
			cat = parts[0];
		}

		String url = "http://attitude.co.uk/" + CAT_CODES.get(cat) + "/page/" + page + "/";
		String html = BaseParser.loadHtml(url, BaseParser.ANDROID_HEADERS, true);
		html = BaseParser.parseContent(html, "<div class=\"gdl-page-item mb0 twelve columns\">", "class='gdl-right-sidebar'>");

		List<RssEntry> entries = new LinkedList<RssEntry>();

		String[] parts = html.split("<div class=\"blog-content-wrapper\">");
		for (int i = 1; i < parts.length - 1; i++) {
			String part = parts[i];
			String link = BaseParser.parseContent(part, "<a href=\"", "\">");
			link = getRssLink(link, cat);
			if (link == null) {
				continue;
			}

			String img = BaseParser.parseContent(part, "<img src=\"", "\"");
			if (img != null) {
				img = "http://bum-util.appspot.com/fetch?url=" + img;
				img = "http://tool.cdn.bum.vn/fetch?code=" + Base64Utils.encode(img);
			}
			part = BaseParser.parseContent(part, "<h2 class=\"blog-title\">", "<div class=\"blog-date\">");

			String title = BaseParser.parseContent(part, "\">", "</a>");
			String intro = BaseParser.parseContent(part, "<div class=\"blog-content\">", "<div class=\"blog-info-wrapper\">");

			RssEntry entry = new RssEntry(link, title);
			entry.setThumb(img);
			entry.setIntro(intro);

			entries.add(entry);
		}

		if (entries.size() >= 9) {
			entries.add(new RssEntry(getCode() + URL_SEPARATOR + cat + PAGE_SEPARATOR + (page + 1), NEXT_PAGE_TITLE));
		}

		if (page > 1) {
			entries.add(0, new RssEntry(getCode() + URL_SEPARATOR + cat + PAGE_SEPARATOR + (page - 1), PREV_PAGE_TITLE));
		}

		return entries;
	}

	@Override
	public String getRssLink(String httpLink) {
		return getRssLink(httpLink, "m");
	}

	// http://attitude.co.uk/ben-whishaw-im-worried-gay-typecasting/ => rss://att/m/ben-whishaw-im-worried-gay-typecasting
	protected String getRssLink(String httpLink, String catId) {
		if (StringUtils.isBlank(httpLink) || !httpLink.startsWith("http://attitude.co.uk/")) {
			return null;
		}

		String code = BaseParser.parseContent(httpLink, "http://attitude.co.uk/", "/");
		if (StringUtils.isBlank(code)) {
			return null;
		}

		return getCode() + URL_SEPARATOR + catId + URL_SEPARATOR + code;
	}

	@Override
	public RssEntry loadEntry(String path, String user) {
		String url = "http://attitude.co.uk/" + path;

		String html = BaseParser.loadHtml(url, BaseParser.ANDROID_HEADERS, true);

		html = BaseParser.parseContent(html, "<h1 class=\"blog-title\">", "<div id=\"taboola-below-article\"></div>");

		String title = BaseParser.parseContent(html, "\">", "</a>");
		html = BaseParser.parseContent(html, "<div class=\"blog-content\">", null);
		if (html != null) {
//			html = html.replace("src=\"http://attitude.co.uk/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif\" data-original=", "src=");
			html = html.replace("src=\"http://attitude.co.uk/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif\"", "");
			html = html.replace("src=\"", "src=\"http://bum-util.appspot.com/fetch?url=");
//			html = html.replace("src=\"", "src=\"http://tool.cdn.bum.vn/fetch?url=");
		}

		RssEntry entry = new RssEntry();
		entry.setTitle(title);
		entry.setContent(html);
		return entry;
	}

	public RssEntry loadEntryWeb(String path, String user) {
		String url = "http://edition.cnn.com/" + path + "/";

		String html = BaseParser.loadHtml(url);
		html = BaseParser.parseContent(html, "<!--startclickprintinclude-->", "<script type=\"text/javascript\">");
		String title = BaseParser.parseContent(html, "<h1>", "</h1>");

		String thumb = BaseParser.parseContent(html, "thumb: '", "',");
		String content = BaseParser.parseContent(html, "<p class=\"cnnEditorialNote\">", null);
		if (StringUtils.isNotBlank(thumb)) {
			content = "<img src=\"" + thumb + "\"/><br/>" + content;
		}

		RssEntry entry = new RssEntry();
		entry.setTitle(title);
		entry.setContent(content);
		return entry;
	}

	public static void main(String[] args) throws Exception {
		AttitudeSite site = new AttitudeSite();
		site.afterPropertiesSet();

		RssEntry entry = site.loadEntry("instagram-gold-a1s-ben-adams-shares-a-shirtless-selfie", null);
		System.out.println(entry.getTitle());
		System.out.println(entry.getContent());

//		List<RssEntry> entries = site.listCatEntrys("1", null);
//		if (entries != null) {
//			System.out.println(entries.size());
//			for (RssEntry e : entries) {
//				System.out.println(e.getUrl() + " " + e.getTitle());
//			}
//		}
	}
}
